package com.xkdx.guangguang.fragment.blog;

import java.util.HashMap;

import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.bhg.BhgActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.WeiboShop;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class BlogHomeFragment extends BaseFragment implements OnClickListener {
	private static final OnCancelListener OnCancelListener = null;
	View view;
	ImageView shop, luxury, wanbiao, card, food, dress, cosmetic, star, digital;
	private String[] tag = { "商场", "奢侈品", "腕表", "信用卡", "美食", "服饰", "化妆品", "名人明星", "数码家居" };
	private String cityID;
	private WeiboShop weiboshop;
	private String token;
	private String shopGroupID;

	ImageView advertise;
	Drawable adverse_dr, shop_dr, luxury_dr, wanbiao_dr, card_dr, food_dr, dress_dr, cosmetic_dr, star_dr, digital_dr;

	SinaBlogAction sinaBlogAction;
	SinaBlogModule sinaBlogModule;
	SinaBlogPresistence sinaBlogPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SharePrefenceUtil su = new SharePrefenceUtil(getActivity(), IConstants.SP_CITY);
		cityID = su.getCityID();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.home_blog, container, false);

		findView();
		setOnClick();
		showLoading();

		return view;
	}

	@Override
	public void findView() {
		shop = (ImageView) view.findViewById(R.id.weibo_shop);
		luxury = (ImageView) view.findViewById(R.id.weibo_luxury);
		wanbiao = (ImageView) view.findViewById(R.id.weibo_wanbiao);
		// aviation = (ImageView) view.findViewById(R.id.weibo_aviation);
		card = (ImageView) view.findViewById(R.id.weibo_card);
		food = (ImageView) view.findViewById(R.id.weibo_food);
		dress = (ImageView) view.findViewById(R.id.weibo_dress);
		cosmetic = (ImageView) view.findViewById(R.id.weibo_cosmetic);
		star = (ImageView) view.findViewById(R.id.weibo_star);
		digital = (ImageView) view.findViewById(R.id.weibo_digital);
		advertise = (ImageView) view.findViewById(R.id.adverse);
		sinaBlogAction = new SinaBlogAction(cityID);
		sinaBlogModule = new SinaBlogModule();

	}

	@Override
	public void setOnClick() {

		SharePrefenceUtil sp = new SharePrefenceUtil(getActivity(), IConstants.SP_WEIBO);
		String city = sp.getWeiboCityID();
		if (city.equals(cityID)) {
			token = sp.getWeiBoToken();
			shopGroupID = sp.getShopWeiBoId();
		} else {
			showLoading();
		}
		shop.setOnClickListener(this);
		luxury.setOnClickListener(this);
		wanbiao.setOnClickListener(this);
		advertise.setOnClickListener(this);
		card.setOnClickListener(this);
		food.setOnClickListener(this);
		dress.setOnClickListener(this);
		cosmetic.setOnClickListener(this);
		star.setOnClickListener(this);
		digital.setOnClickListener(this);

	}

	@Override
	public void showLoading() {

		sinaBlogPresistence = new SinaBlogPresistence(this);
		((ManagerActivity) getActivity()).addCurrentTask(sinaBlogPresistence);
		sinaBlogPresistence.setActitons(sinaBlogAction);
		sinaBlogPresistence.setModule(sinaBlogModule);
		sinaBlogPresistence.execute();
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		if (map.containsKey("0")) {
			weiboshop = sinaBlogModule.weiboShop;
			setValuse();
		} else {
			showErrorTip(map);
		}

	}

	private void setValuse() {
		token = weiboshop.getDetailInfo().getAccessToken();
		shopGroupID = weiboshop.getDetailInfo().getShopWeibo();
		SharePrefenceUtil sp = new SharePrefenceUtil(getActivity(), IConstants.SP_WEIBO);
		sp.clear();
		sp.setWeiBoToken(token);
		sp.setShopWeiBoId(shopGroupID);
		sp.setWeiboCityID(cityID);
	}

	private void setFragment(int i) {

		FragmentTransaction ft = getFragmentManager().beginTransaction();
		BlogFragment blogFragment = new BlogFragment();
		Bundle b = new Bundle();

		b.putString("tag", tag[i]);
		if (i == 0) {
			SharePrefenceUtil sp = new SharePrefenceUtil(getActivity(), IConstants.SP_WEIBO);
			shopGroupID = sp.getShopWeiBoId();
			b.putLong("groupID", Long.parseLong(shopGroupID));
		} else if (i == 1) {
			b.putLong("groupID", 3543099730893027L);
		} else if (i == 2) {
			b.putLong("groupID", 3537611287520837L);
		} else if (i == 3) {
			b.putLong("groupID", 3537610478109012L);
		} else if (i == 4) {
			b.putLong("groupID", 3537611379676496L);
		} else if (i == 5) {
			b.putLong("groupID", 3537611115383888L);
		} else if (i == 6) {
			b.putLong("groupID", 3537619121761488L);
		} else if (i == 7) {
			b.putLong("groupID", 3526780776286686L);
		} else if (i == 8) {
			b.putLong("groupID", 3526780775924222L);
		}
		b.putString("token", token);
		// b.putString("token", "2.00hBHaUDGw3u_Bb2f0d3f89cnDoryC");
		// Log.i("token", token);
		blogFragment.setArguments(b);
		ft.replace(R.id.fragment, blogFragment);
		ft.addToBackStack(null);
		ft.commit();
	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.weibo_shop:
			setFragment(0);
			break;
		case R.id.weibo_luxury:
			setFragment(1);
			break;
		case R.id.weibo_wanbiao:
			setFragment(2);
			break;
		case R.id.weibo_card:
			setFragment(3);
			break;
		case R.id.weibo_food:
			setFragment(4);
			break;
		case R.id.weibo_dress:
			setFragment(5);
			break;
		case R.id.weibo_cosmetic:
			setFragment(6);
			break;
		case R.id.weibo_star:
			setFragment(7);
			break;
		case R.id.weibo_digital:
			setFragment(8);
			break;

		case R.id.adverse:
			if (getActivity() != null) {
				Intent intent = new Intent(getActivity(), BhgActivity.class);
				startActivity(intent);
			}
			break;
		default:
			break;
		}

	}

}
